import { combineReducers } from 'redux';
import {
  SET_UPDATE_USER,

  UUID_ASYNC,
  LOGIN,
  GET_MEETINGROOM,
  GET_BUSINESS,
  GET_ALLUSERS
} from './actions';

export interface PublicState {
  imgCode: { uuid: string, data: ArrayBuffer } // 图片验证啊
  login: { loginLoading: boolean, res: any } // 登录
  meetingRoom: { meetingRoomLoading: boolean, res: any } // 会议室
  business: { businessLoading: boolean, res: any } // 意向商家
  users: { usersLoading: boolean, res: any[] } // 用户列表
  updateUser: any // 当前修改用户
}
const publicState: PublicState = {
  imgCode: {
    uuid: '',
    data: new ArrayBuffer(0)
  },
  // 登录
  login: {
    loginLoading: false,
    res: null
  },
  // 会议室
  meetingRoom: { meetingRoomLoading: false, res: null },
  // 意向商家
  business: { businessLoading: false, res: null },
  // 用户列表
  users: { usersLoading: false, res: [] },
  // 当前修改用户
  updateUser: {}
}

/**
 * uuid
 */
const reducer_uuid = (
  state = publicState.imgCode,
  action: ReduxActionObject
) => {
  switch (action.type) {
    case `${UUID_ASYNC}_PENDING`: return null;
    case `${UUID_ASYNC}_FULFILLED`: 
      return {
        uuid: action.payload.header.uuid,
        data: action.payload.data
      };
    case `${UUID_ASYNC}_REJECTED`: return null;
    default: return state;
  }
}

/**
 * login
 */
const reducer_login = (
  state = publicState.login,
  action: ReduxActionObject
) => {
  switch (action.type) {
    case `${LOGIN}_PENDING`: return action.payload;
    case `${LOGIN}_FULFILLED`:
    case `${LOGIN}_REJECTED`: return { loginLoading: false, res: action.payload };
    default: return state;
  }
}

/**
 * meetingRoom
 */
const reducer_meetingRoom = (
  state = publicState.meetingRoom,
  action: ReduxActionObject
) => {
  switch (action.type) {
    case `${GET_MEETINGROOM}_PENDING`: return action.payload;
    case `${GET_MEETINGROOM}_FULFILLED`:
    case `${GET_MEETINGROOM}_REJECTED`: return { meetingRoomLoading: false, res: action.payload };
    default: return state;
  }
}

/**
 * business
 */
const reducer_business = (
  state = publicState.business,
  action: ReduxActionObject
) => {
  switch (action.type) {
    case `${GET_BUSINESS}_PENDING`: return action.payload;
    case `${GET_BUSINESS}_FULFILLED`:
    case `${GET_BUSINESS}_REJECTED`: return { businessLoading : false, res: action.payload };
    default: return state;
  }
}

/**
 * users
 */
const reducer_users = (
  state = publicState.users,
  action: ReduxActionObject
) => {
  switch (action.type) {
    case `${GET_ALLUSERS}_PENDING`: return action.payload;
    case `${GET_ALLUSERS}_FULFILLED`:
    case `${GET_ALLUSERS}_REJECTED`: return { usersLoading : false, res: action.payload };
    default: return state;
  }
}
// 选择修改用户
const reducer_set_update_user = (
  state = publicState.updateUser,
  action: ReduxActionObject
) => {
  switch (action.type) {
    case SET_UPDATE_USER: return action.user;
    default: return state;
  }
}

export default combineReducers({
  imgCode: reducer_uuid,
  login: reducer_login,
  meetingRoom: reducer_meetingRoom,
  business: reducer_business,
  users: reducer_users,
  updateUser: reducer_set_update_user
})